// 商品渲染
// shopping() 
function getgoods() {
	let vipname = getCookie("user");
	$.get("php/getShoppingCart.php?vipName=" + vipname, function(data) {
		// console.log(data);
		// let goodsId = location.search.split("=")[1];
		// console.log(goodsId);
		let goodsList = JSON.parse(data);
		// console.log("goodsList",goodsList);
		let htmlStr = "";
		let index = 0;
		goodsList.forEach(goods => {
			// console.log(goods);
			index++;
			htmlStr = `
			<tr id="otr${index}">
				<td class="commodity">
					<div id="comm-img">
						<img src="${goods.goodsImg}" >
					</div>
					<div id="comm-cat">
						<h3>${goods.beiyong1}</h3>
						<p class="type">C880100102186</p>
					</div>
					
				</td>
				<td>
					<button class="jian">-</button>
					<span class="goods-num">${goods.goodsCount}</span>
					<button class="jian">+</button>
				</td>
				<td>
					单价:<span class="goods-unit-price">${goods.goodsPrice}</span>
				</td>
				<td>
					小计:<span class="goods-single-price">${goods.goodsPrice}</span>
				</td>
				<td>
					操作:<input type="button" value="x" class="putt"/>
				</td>
			</tr>
			`;

			$("#box").append(htmlStr);
		});
		shopping()
	});
}



// 购物车操作
function shopping() {

	class Cart {
		constructor() {
			this.eventBind();
		}
		getGoodsNumAndUpdate() {
			console.log(1);
			let oGoodsNum = document.getElementsByClassName("goods-num");
			let oGoodsTotalNum = document.getElementsByClassName("goods-total-num")[0];
			let goodsTotalNum = 0;

			for (let i = 0; i < oGoodsNum.length; i++) {
				goodsTotalNum += Number(oGoodsNum[i].innerHTML);
			}

			oGoodsTotalNum.innerHTML = goodsTotalNum;
		}


		getGoodsPriceAndUpdate() {
			console.log(1);
			let oGoodsSinglePrice = document.getElementsByClassName("goods-single-price");
			let oGoodsTotalPrice = document.getElementsByClassName("goods-total-price")[0];
			let goodsTotalPrice = 0;

			for (let i = 0; i < oGoodsSinglePrice.length; i++) {
				goodsTotalPrice += Number(oGoodsSinglePrice[i].innerHTML);
			}

			oGoodsTotalPrice.innerHTML = goodsTotalPrice.toFixed(2);
		}


		getSinglePrice(num, price) {
			return num * price;
		}


		addGoods(btn) {

			let oGoodsNum = btn.previousElementSibling;
			oGoodsNum.innerHTML = Number(oGoodsNum.innerHTML) + 1;
			let oGoodsUnitPrice = btn.parentNode.nextElementSibling.firstElementChild;
			let oGoodsSinglePrice = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
			oGoodsSinglePrice.innerHTML = this.getSinglePrice(oGoodsNum.innerHTML, oGoodsUnitPrice.innerHTML).toFixed(2);
			let otr = btn.parentNode.parentNode.parentNode.children;
			let otrs = btn.parentNode.parentNode;
			for (let i = 0; i < otr.length; i++) {
				if (otr[i].id == otrs.id) {
					let vipname = getCookie("user");
					ajax({
						type: 'GET',
						url: 'php/getShoppingCart.php',
						data: {
							vipName: vipname,
						},
						isAsync: true,
						success: function(res) {
							let zhi = JSON.parse(res)
							ajax({
								type: 'GET',
								url: 'php/updateGoodsCount.php',
								data: {
									vipName: vipname,
									goodsId: zhi[i].goodsId,
									goodsCount: oGoodsNum.innerText,
								},
								isAsync: true,
								success: function(res) {
									if (res == 1) {
									console.log(1);
									} else {
										alert("修改失败")
									}
								}
							})
						}
					});
				}
			}

			this.getGoodsNumAndUpdate();

			this.getGoodsPriceAndUpdate();
		}

		minGoods(btn) {
			let oGoodsNum = btn.nextElementSibling;

			if (oGoodsNum.innerHTML > 0) {
				oGoodsNum.innerHTML = Number(oGoodsNum.innerHTML) - 1;
				let oGoodsUnitPrice = btn.parentNode.nextElementSibling.firstElementChild;
				let oGoodsSinglePrice = btn.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
				oGoodsSinglePrice.innerHTML = this.getSinglePrice(oGoodsNum.innerHTML, oGoodsUnitPrice.innerHTML).toFixed(2);

				let otr = btn.parentNode.parentNode.parentNode.children;
				let otrs = btn.parentNode.parentNode;
				for (let i = 0; i < otr.length; i++) {
					if (otr[i].id == otrs.id) {
						let vipname = getCookie("user");
						ajax({
							type: 'GET',
							url: 'php/getShoppingCart.php',
							data: {
								vipName: vipname,
							},
							isAsync: true,
							success: function(res) {
								let zhi = JSON.parse(res)
								ajax({
									type: 'GET',
									url: 'php/updateGoodsCount.php',
									data: {
										vipName: vipname,
										goodsId: zhi[i].goodsId,
										goodsCount: oGoodsNum.innerText,
									},
									isAsync: true,
									success: function(res) {
										if (res == 1) {
											console.log(1);
										} else {
											console.log(0);
										}
									}
								})
							}
						});
					}
				}
				
				this.getGoodsNumAndUpdate();

				this.getGoodsPriceAndUpdate();
			}
		}

		delGoods(btn) {



			let putt = document.querySelectorAll('.putt');
			for (let j = 0; j < putt.length; j++) {
				putt[j].onclick = function() {
					if(confirm("请确定要将该商品删除吗？")){
						let otrrr = this.parentNode.parentNode;
						let vipname = getCookie("user");
						ajax({
							type: 'GET',
							url: 'php/getShoppingCart.php',
							data: {
								vipName: vipname,
							},
							isAsync: true,
							success: function(res) {
								let zhi = JSON.parse(res)
								ajax({
									type: 'GET',
									url: 'php/deleteGoods.php',
									data: {
										vipName: vipname,
										goodsId: zhi[j].goodsId,
									},
									isAsync: true,
									success: function(res) {
										if (res == 1) {
											alert('删除成功');
											
											otrrr.remove();
										} else {
											alert("删除失败")
										}
									}
								})
							},
						});
						
						
					};
					let oTr = btn.parentNode.parentNode;
					
					oTr.remove();
				
					
				};
				
				
			};
			this.getGoodsNumAndUpdate();
			console.log()
			this.getGoodsPriceAndUpdate();
		}
						
		eventBind() {
			let oBtns = document.querySelectorAll(".jian");
			console.log(oBtns)
			let that = this;
			for (let i = 0; i < oBtns.length; i++) {
				if (i % 2 == 0) {
					oBtns[i].onclick = function() {
						that.minGoods(this);
					}
				} else {
					oBtns[i].onclick = function() {
						that.addGoods(this);
					}
				}
			}

			let oDelBtns = document.getElementsByTagName("input");

			for (let i = 0; i < oDelBtns.length; i++) {
				oDelBtns[i].onclick = function() {
					that.delGoods(this);
				}
			}
		}
		eventBinds() {
			this.getGoodsNumAndUpdate();

			this.getGoodsPriceAndUpdate();
		}
	}


	let c = new Cart();
	let add = document.querySelector("#box");
	c.eventBinds();
}

$(function() {
	getgoods();
	shopping()
});
